from django.shortcuts import render,redirect
from django.core.paginator import Paginator
from django.contrib.auth import login,authenticate
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from app.models import Users,LeaveC,Leave

# Create your views here.
# 注册
def Logns(request):
    if request.method=='POST':
        username=request.POST.get('username')
        password=request.POST.get('password')
        pwd=request.POST.get('pwd')
        role=request.POST.get('role')
        password=password and pwd
        user=User.username
        if username==user:
            return render(request,'logns.html',{'error':'此用户已存在'})
        User.objects.create_user(username=username,password=password)
        Users.objects.create(uname=username,upwd=password,role=role)
        return redirect('log')
    return render(request, 'logns.html',)
# 登录
def Log(request):
    if request.method=='POST':
        username=request.POST.get('username')
        password=request.POST.get('password')
        role=request.POST.get('role')
        user=authenticate(username=username,password=password)
        if role=='1':
            if user:
                login(request, user)
                return redirect('add')
            return render(request,'log.html',{'error':'用户名或密码错误'})
        if user:
            login(request,user)
            return redirect('index')
        return render(request, 'log.html', {'error': '用户名或密码错误'})
    return render(request, 'log.html')
# 添加
def Add(request):
    leavec = LeaveC.objects.all()
    if request.method=='POST':
        stu_name = request.POST.get('stu_name')
        status = request.POST.get('status')
        createTime = request.POST.get('createTime')
        tid = request.POST.get('tid')
        Leave.objects.create(stu_name=stu_name,status=status,createTime=createTime,tid=tid)
        return redirect('add')
    return render(request, 'add.html',{'leavec':leavec})
# 展示
def Index(request):
    leave=Leave.objects.all()
    leavec=LeaveC.objects.all()
    page=int(request.GET.get('page',1))
    p = Paginator(leave, 3)
    allpage = p.num_pages
    nowpage = p.get_page(page)
    pre_page = nowpage.number - 1
    next_page = nowpage.number + 1
    return render(request, 'index.html', {'leave': nowpage.object_list,
                                          'allpage': allpage,
                                          'pre_page': pre_page,
                                          'next_page': next_page,
                                          'nowpage': nowpage.number,
                                          'leavec':leavec
                                          })
# 过滤
def seeks(request):
    if request.method=="POST":
        tname=request.POST.get('tname')
        leave=LeaveC.objects.get(tname=tname).leave_set().all()
        page = int(request.GET.get('page', 1))
        p = Paginator(leave, 3)
        allpage = p.num_pages
        nowpage = p.get_page(page)
        pre_page = nowpage.number - 1
        next_page = nowpage.number + 1
        return render(request, 'seeks.html', {'leave': nowpage.object_list,
                                              'allpage': allpage,
                                              'pre_page': pre_page,
                                              'next_page': next_page,
                                              'nowpage': nowpage.number,
                                              })